<?php

namespace App\Console;

use App\Models\Product;
use App\Models\ProductAuction;
use App\Models\SystemSeting;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {

        $schedule->call(function (){
            collect(ProductAuction::whereIn('status',[0,1])->get())->map(function ($item){

                if ($item->start < date('H:i:s')){

                    $flag = ProductAuction::find($item->id);

                    if (date('H:i:s',strtotime('+'.$item->continue.'minute',$item->start)) > date('H:i:s')){

                        $flag->status = 1;
                    }else{

                        $flag->status = 2;
                    }

                    $flag->save();
                }

            });
        })->everyMinute();

        $schedule->call(function (){

            //商品增值
            collect(Product::where('pick',0)->get())->map(function ($item){

                $setting = SystemSeting::find('product')->data;

                $flag = Product::find($item->id);
                $flag->price = $flag->price + $flag->increase;
                $flag->intgral = round($flag->price * $setting['integral'] /100,2);
                $flag->increase = round($flag->price * $setting['increase'] /100,2);
                $flag->save();

            });

            //自动添加拍卖
            $auto = SystemSeting::find('product_auto');

            $auto_time = explode('|',$auto->data['time']);

            foreach ($auto_time as $item){

                ProductAuction::create([
                    'start'     => $item,
                    'continue'  => $auto->data['continue']
                ]);
            }

        })->daily();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}
